﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Collections.Generic;

using Security;


namespace TripLOA.GUI.secure
{
    public partial class CreatedTrips : TripLOA.GUI.internationalization.LOABasePage
    {
        List<Groupware.Trip> createdtrips;
        TripLoaUser us;

        protected void Page_Load(object sender, EventArgs e)
        {
            us = TripLoaUserManager.Instance.getUser(User.Identity.Name);
            createdtrips = Groupware.TripManager.GetAllTripByUserID(us.UserID);

            GridViewCreatedTrips.Columns[4].HeaderText = GetLocalResourceObject("countReq").ToString();
           
            GridViewCreatedTrips.DataSource = createdtrips;
            GridViewCreatedTrips.DataBind();

            if (createdtrips.Count > 0)
            {
                foreach (GridViewRow r in GridViewCreatedTrips.Rows)
                {
                    int n = createdtrips[r.RowIndex].GetPendingReservationRequests(us).Count();
                    if (n != 0)
                    {
                        Label lb = (Label)GridViewCreatedTrips.Rows[r.RowIndex].FindControl("lblcount");
                        lb.Text = n.ToString();
                        GridViewCreatedTrips.Columns[4].Visible = true;
                    }
                }
            }
        }

        protected void GridViewCreatedTrips_OnSelectedIndexChanging(Object sender, GridViewSelectEventArgs e)
        {
            DataKey selectedTrip = GridViewCreatedTrips.DataKeys[e.NewSelectedIndex];
            Response.Redirect("tripDetail.aspx?id=" + selectedTrip.Values[0].ToString());

        }

        protected void GridViewCreatedTrips_OnPageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewCreatedTrips.PageIndex = e.NewPageIndex;            
            Object prova = GridViewCreatedTrips.DataSource;
            GridViewCreatedTrips.DataBind();

            GridViewCreatedTrips.Columns[4].Visible = false;
            int tripIndex = GridViewCreatedTrips.PageIndex * GridViewCreatedTrips.PageSize;

            foreach (GridViewRow r in GridViewCreatedTrips.Rows)
            {
                int n = createdtrips[tripIndex].GetPendingReservationRequests(us).Count();
                if (n != 0)
                {
                    Label lb = (Label)GridViewCreatedTrips.Rows[r.RowIndex].FindControl("lblcount");
                    lb.Text = n.ToString();
                    GridViewCreatedTrips.Columns[4].Visible = true;
                }
                tripIndex++;
            }

        }

        protected void GridViewCreatedTrips_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
